/*
 * @lc app=leetcode.cn id=107 lang=cpp
 *
 * [107] 二叉树的层序遍历 II
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void perorder(vector<vector<int>> &res,int depth,TreeNode *root)
    {
        if(!root)
            return;
        if(depth>=res.size())
            res.push_back(vector<int> {});
        res[depth].push_back(root->val);
        perorder(res,depth+1,root->left);
        perorder(res,depth+1,root->right);
    }
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> res;
        perorder(res,0,root);
        reverse(res.begin(),res.end());
        return res;
    }
    

};
// @lc code=end

